/* Copyright (c) 2008-2011 -- CommonsWare, LLC

	 Licensed under the Apache License, Version 2.0 (the "License");
	 you may not use this file except in compliance with the License.
	 You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

	 Unless required by applicable law or agreed to in writing, software
	 distributed under the License is distributed on an "AS IS" BASIS,
	 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 See the License for the specific language governing permissions and
	 limitations under the License.
*/
	 
package com.jun2yah.quiz;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * com.jun2yah.quiz
 * DatabaseHelper.java
 *
 * @author jun2yah
 * @date 2011. 6. 11. 오전 2:01:22
 * @Version
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	static final String DATABASE_NAME = "words.db";
	static final String TABLE_NAME = "words";
	static final String WORD = "word";
	static final String MEAN = "mean";
	static final String GROUP = "group";
	
	
	/**
	 * 
	 * @Constructor DatabaseHelper
	 * @param context
	 *
	 * @author jun2yah
	 * @date 2011. 6. 11. 오전 2:01:13
	 */
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, 1);
	}
	
	/**
	 * 
	 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
	 * @Method onCreate
	 * @param db
	 *
	 * @author jun2yah
	 * @date 2011. 6. 11. 오전 2:01:17
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
//		db.execSQL("CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + WORD + " TEXT, " + MEAN + " TEXT);");
//		
//		ContentValues cv = new ContentValues();
//		
//		cv.put(WORD, "trade");
//		cv.put(MEAN, "무역");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "patent");
//		cv.put(MEAN, "특허");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "theft");
//		cv.put(MEAN, "도둑질");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "massive");
//		cv.put(MEAN, "크고 묵직한");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "relation");
//		cv.put(MEAN, "관계/관련");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "severe");
//		cv.put(MEAN, "엄한/엄격한");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "coverage");
//		cv.put(MEAN, "적용 범위");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "expand");
//		cv.put(MEAN, "확대하다");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "volunteer");
//		cv.put(MEAN, "지원하다");
//		db.insert(TABLE_NAME, WORD, cv);
//		
//		cv.put(WORD, "request");
//		cv.put(MEAN, "요구/주장");
//		db.insert(TABLE_NAME, WORD, cv);
	}

	/**
	 * 
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 * @Method onUpgrade
	 * @param db
	 * @param oldVersion
	 * @param newVersion
	 *
	 * @author jun2yah
	 * @date 2011. 6. 11. 오전 2:01:30
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
		onCreate(db);
	}
	
	/**
	 * DB를 assets에서 sdcard로 복사한다.
	 * 
	 * @Method copyDB
	 * @param context
	 *
	 * @author jun2yah
	 * @date 2011. 6. 11. 오전 2:27:53
	 */
	public void copyDB(Context context) {
		AssetManager manager = context.getAssets();
		String filePath = "/data/data/" + context.getPackageName() + "/databases/" + DATABASE_NAME;
		File file = new File(filePath);
		
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		
		try {
			InputStream is = manager.open(DATABASE_NAME);
			BufferedInputStream bis = new BufferedInputStream(is);
			
			if (file.exists()) {
				file.delete();
				file.createNewFile();
			}
			
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			
			int read = -1;
			byte[] buffer = new byte[1024];
			while ((read = bis.read(buffer, 0, 1024)) != -1) {
				bos.write(buffer, 0, read);
			}
			bos.flush();
			
			bos.close();
			fos.close();
			bis.close();
			is.close();
		} catch (IOException e) {
			Log.e("jhkim", e.getMessage());
		}
	}
}